package com.kaoyaya.delivery.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EFragment;
import com.googlecode.androidannotations.annotations.ItemClick;
import com.googlecode.androidannotations.annotations.ViewById;
import com.googlecode.androidannotations.annotations.sharedpreferences.Pref;
import com.kaoyaya.delivery.activity.R;
import com.kaoyaya.delivery.activity.SlidingActivity;
import com.kaoyaya.delivery.activity.UserCenterActivity_;
import com.kaoyaya.delivery.adapter.SideBarAdapter;
import com.kaoyaya.delivery.data.Constants;
import com.kaoyaya.delivery.data.DataHelper;
import com.kaoyaya.delivery.data.InfoFile_;
import com.kaoyaya.util.StringUtil;

import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

@EFragment(R.layout.fragment_left)
public class LeftFragment extends Fragment {

	@ViewById(R.id.lv_sidebar) ListView lvSidebar;
	@ViewById(R.id.iv_avatar) ImageView ivAvatar;
	@ViewById(R.id.tv_username) TextView tvTitle;
	@ViewById LinearLayout llSidebarAccount;
	private List<SideBarItem> sideBarItems;
	private LayoutInflater inflater;
	private SlidingActivity activity;
	private Map<String, SideBarCategory> sideBarMap;
	@Bean DataHelper dataHelper;
	@Pref InfoFile_ infoFile;
	final int IS_GOLD = 0;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		this.inflater = inflater;
		this.activity = (SlidingActivity) getActivity();
		this.sideBarMap = new HashMap<String, SideBarCategory>();
		return null;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@AfterViews
	public void calledAfterViewInjection() {
		dataHelper.setCallbackHandler(callbackHandler);
		dataHelper.getUserAccount(IS_GOLD);
		if (!Constants.isLogin) {
			tvTitle.setText("个人中心");
		}
		if (sideBarItems == null) {
			initSideBarMenu();
		}
	}
	
	@Click
	void llSidebarAccount() {
		startActivity(new Intent(activity, UserCenterActivity_.class));
	}
	
	Handler callbackHandler = new Handler() {
		public void handleMessage(Message msg) {
			Object result = msg.obj;
			switch (msg.what) {
			case IS_GOLD:
				Constants.gold = (Double) result;
				updateBalance();
				break;
			}
		}
	};

	/**
	 * 初始化菜单
	 * @author Goven
	 * @date 2013-5-19
	 */
	private void initSideBarMenu() {
		List<SideBarItem> sideBarItems = new ArrayList<SideBarItem>();
		String fmtTag = null;
		SideBarCategory sideBarCategory = null;
		
		sideBarItems.add(new SideBarTitle(inflater, getResources().getString(R.string.sidebar_delivery)));
		TypedArray icons = getResources().obtainTypedArray(R.array.sidebar_delivery_icons);
		String[] titles = getResources().getStringArray(R.array.sidebar_delivery_titles);
		String[] tags = getResources().getStringArray(R.array.sidebar_delivery_tags);
		for (int i = 0; i < titles.length; i++) {
			fmtTag = tags[i];
			sideBarCategory = new SideBarCategory(fmtTag, inflater, icons.getResourceId(i, -1), titles[i]);
			sideBarItems.add(sideBarCategory);
			sideBarMap.put(fmtTag, sideBarCategory);
		}

		sideBarItems.add(new SideBarTitle(inflater, getResources().getString(R.string.sidebar_recharge)));
		icons = getResources().obtainTypedArray(R.array.sidebar_recharge_icons);
		titles = getResources().getStringArray(R.array.sidebar_recharge_titles);
		tags = getResources().getStringArray(R.array.sidebar_recharge_tags);
		for (int i = 0; i < titles.length; i++) {
			fmtTag = tags[i];
			sideBarCategory = new SideBarCategory(fmtTag, inflater, icons.getResourceId(i, -1), titles[i]);
			sideBarItems.add(sideBarCategory);
			sideBarMap.put(fmtTag, sideBarCategory);
		}

		sideBarItems.add(new SideBarTitle(inflater, getResources().getString(R.string.sidebar_history)));
		icons = getResources().obtainTypedArray(R.array.sidebar_history_icons);
		titles = getResources().getStringArray(R.array.sidebar_history_titles);
		tags = getResources().getStringArray(R.array.sidebar_history_tags);
		for (int i = 0; i < titles.length; i++) {
			fmtTag = tags[i];
			sideBarCategory = new SideBarCategory(fmtTag, inflater, icons.getResourceId(i, -1), titles[i]);
			sideBarItems.add(sideBarCategory);
			sideBarMap.put(fmtTag, sideBarCategory);
		}
		lvSidebar.setAdapter(new SideBarAdapter(sideBarItems));
		updateCart();
	}
	
	public void updateBalance() {
		SideBarCategory sideBarCategory = sideBarMap.get("balance");
		View view = sideBarCategory.getView();
		TextView tvTip = (TextView) view.findViewById(R.id.tvTip);
		if (Constants.gold > 0.0) {
			tvTip.setVisibility(View.VISIBLE);
			tvTip.setText(Constants.gold + "点");
		} else {
			tvTip.setVisibility(View.GONE);
		}
	}
	
	public void updateCart() {
		SideBarCategory sideBarCategory = sideBarMap.get("cart");
		View view = sideBarCategory.getView();
		TextView tvTip = (TextView) view.findViewById(R.id.tvTip);
		int orderCount = Constants.cart.size();
		if (orderCount != 0) {
			tvTip.setVisibility(View.VISIBLE);
			tvTip.setText(String.valueOf(orderCount));
		} else {
			tvTip.setVisibility(View.GONE);
		}
	}

	@ItemClick(R.id.lv_sidebar)
	public void itemClick(SideBarItem item) {
		if (item instanceof SideBarCategory) {
			if (StringUtil.isNotTrimBlank(item.getTag()) ) {
				activity.updateContent(item.getTag(), true);
			}
		}
	}

}
